<?php
/**
 * @Author: king
 * @Last Modified by:   king
 * Date: 2018/6/19
 * Time: 15:47
 */

namespace App\Http\Controllers\Api\v1\Pay;

use App\Http\Controllers\Api\v1\Order\OrdersPayController;
use App\Exceptions\ApiException;
use App\Http\Controllers\Api\BaseController;
use Illuminate\Http\Request;

/**
 * @title 支付相关接口
 * @desc 支付宝支付、微信支付、京东支付
 * @url
 * @version 1.1.1
 * Class Ali
 * @package app\api\controller\v2
 */
class CommonController extends BaseController
{
    protected $restActionList = ['save'];

    /**
     * 下单(预支付订单)
     * @method POST
     * @desc /pay/order
     * @return array
     * @throws ApiException
     * @throws db\exception\DataNotFoundException
     * @throws db\exception\ModelNotFoundException
     * @throws exception\DbException
     * @readme /doc/md/pay/save.md
     */
    public function save(Request $request)
    {
        if (!$request->isMethod('post')) {
            return show(config('code.error'), '未知操作', [], 403);
        }

        $trade_type   = $request->input('trade_type'); // 支付方式
        $gid          = $request->input('gid'); // 商品类型id   1=>生时校正
        $id           = $request->input('id') ?? 0; // 商品id =>生时校正id
        $openid       = $request->input('openid', '');
        $callback_url = $request->input('callback_url', '');

        if (!$id) {
            return show(config('code.error'), '参数错误', [], 400);
        }

        //下单统一
        $OrderPayModel = new OrdersPayController();

        $data = $OrderPayModel->pay_unlock(['order_type' => 1, 'trade_type' => $trade_type, 'gid' => $gid, 'reid' => $id, 'openid' => $openid, 'callback_url' => $callback_url]);

        if ($data['status'] === true) {
            if ($trade_type == 1) {
                return show(config('code.success'), '下单成功', $data['data']['pay_url']);
            } else {
                return show(config('code.success'), '下单成功', $data['data']);
            }

        } else {
            return show(config('code.error'), '下单失败');
        }
    }

    /**
     * 参数规则
     * @name //字段名称
     * @type //类型
     * @require 是否必须
     * @default 默认值
     * @desc 说明
     * @range 范围
     * @return array
     */
    public static function getRules()
    {
        $rules = [
            'save' => [
                'access_token' => ['name' => 'access_token', 'type' => 'string', 'require' => 'true', 'default' => '无', 'desc' => '接口访问凭证', 'range' => '32位'],
                'ucid'         => ['name' => 'ucid', 'type' => 'string', 'require' => 'true', 'default' => '无', 'desc' => '用户ucid', 'range' => '32位'],
                'trade_type'   => ['name' => 'trade_type', 'type' => 'int', 'require' => 'true', 'default' => '无', 'desc' => '支付方式', 'range' => '1=>支付宝,2=>微信支付，3=>手机网页，4=>电脑网页'],
                'gid'          => ['name' => 'gid', 'type' => 'int', 'require' => 'true', 'default' => '无', 'desc' => '商品类型', 'range' => '1=>生时校正'],
                'id'           => ['name' => 'id', 'type' => 'int', 'require' => 'true', 'default' => '无', 'desc' => '商品id即生时校正id', 'range' => ''],
            ],
        ];
        return $rules;
    }
}
